home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10328 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  147 lines

  1. Path: news.nask.org.pl!usenet
  2. From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can somebody explain me how to do this in C++
  5. Date: Wed, 06 Mar 1996 23:39:02 GMT
  6. Organization: Research and Academic Computer Network
  7. Message-ID: <4hl7ib$cpi@bilbo.nask.org.pl>
  8. References: <4hkb2o$ko3@mo6.rc.tudelft.nl>
  9. NNTP-Posting-Host: s110.maloka.waw.pl
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Ejo Schrama <schrama@geo.tudelft.nl> wrote:
  13.  
  14. >Suppose you've got a base class containing protected data and several 
  15. >public methods among which there are some virtual ones. This base class
  16. >is inhereted to "lets call them" subclasses in which the virtual methods
  17. >are implemented. All subclasses are defined during the initialization of a
  18. >problem, however during the execution of a program "where all work is
  19. >executed" you only want to deal with a linked list of base class objects
  20. >which are accessed via a while loop. The code looks as follows:
  21.  
  22. >  Cparam *localpointer = root_of_linked_list;
  23. >  while (localpointer != NULL) {
  24. >    localpointer->execute_some_virtual_method();
  25. >    localpointer = localpointer->getlinktonext();
  26. >  }
  27.  
  28. >So far there is no problem until the following situation happens. There are 
  29. >local variables declared as protected data inside the inhereted subclasses 
  30. >(which don't exist in the baseclass) and I want to access those variables
  31. >in the while construction described above. 
  32.  
  33. >Any attempt I do to declare
  34.  
  35. >  Cparam_inhereted_method *localpointer2 = localpointer; 
  36.  
  37. >will always fail since:
  38.  
  39. >  class Cparam {
  40. >    protected:
  41. >      <various data>
  42. >    public:
  43. >      <various constructors/destructors and methods except execute_routine>
  44. >      virtual void execute_some_virtual_method();
  45. >  };
  46.  
  47. >  class Cparam_inhereted_method : public Cparam {
  48. >    protected:
  49. >      <various data>
  50. >    public:
  51. >      <constructors/destructors>
  52. >      void execute_some_virtual_method();
  53. >      void execute_routine();
  54. >  };
  55.  
  56. >so that I can never execute
  57.  
  58. >  localpointer2->execute_routine();
  59.  
  60. >The only alternative seems to introduce execute_routine to the Cparam
  61. >base class (which I would like to avoid). How can I avoid this?
  62.  
  63. If I understand your example correctly it seems that all you were
  64. missing was an explicit type cast to the derived type.
  65. >  Cparam_inhereted_method *localpointer2 = localpointer; 
  66. would work as :
  67. >  Cparam_inhereted_method *localpointer2 = (Cparam_inhereted_method *)localpointer; 
  68.  
  69. However this can be risky, if you have no way of selecting the correct
  70. derived type.
  71. Here is a working example (BC4.51)
  72.  
  73.  
  74. #include <stdio.h>
  75.  
  76. class CBase {
  77. protected:
  78. char      d ;
  79. public:
  80. void m1( ) {
  81.     printf("m1 in base\n") ;
  82.     }
  83.  
  84. virtual void m2( ) {
  85.     printf("m2 in base\n");
  86.     }
  87. }  ;
  88.  
  89. class CDer : CBase {
  90. protected:
  91. char e;
  92. public:
  93. void m3( ) {
  94.     printf("M3 in Derived\n");
  95.     }
  96.  
  97. void m2()  {
  98.      printf("M2 in Derived...\n");
  99.     }
  100.  
  101. };
  102.  
  103.  
  104. class CDer2 : CBase {
  105. protected:
  106. char f;
  107. int  g ;
  108.  
  109. public:
  110. void m3( ) {
  111.     printf("M3 in Derived2\n");
  112.     }
  113.  
  114. void m2()  {
  115.      printf("M2 in Derived2...\n");
  116.     }
  117.  
  118. };
  119.  
  120.  
  121.  
  122. int main( int argc, char **argv ) {
  123.  
  124. CDer cd ; // cd will be object of derived type
  125. CDer2 cd2 ;
  126.  
  127. CBase *cb = (CBase*)&cd ; // cb will be generic base class pointer
  128. CDer *cdp = (CDer *)cb ; // now do your cast to more complex (derived)
  129. type
  130. cdp->m2(); // check if virtual function knows we are really of derived
  131. type
  132. cdp->m3(); // check if function existing only in derived works
  133.  
  134. cb = (CBase*)&cd2 ; // cb will be generic base class pointer
  135. CDer2 *cd2p = (CDer2 *)cb ; // now do your cast to more complex
  136. (derived) type
  137. cd2p->m2(); // check if virtual function knows we are really of
  138. derived type
  139. cd2p->m3(); // check if function existing only in derived works
  140. }
  141.  
  142. Piotr Parlewicz
  143. (piotrpar@blue.maloka.waw.pl)
  144.  
  145.  
  146.  
  147.